<template>
	
	<view>
		<view class="img_list">
			<view class="delbar" :style="{height:isdel?'100rpx':'0'}">
				<view @tap='cancel_del' >取消</view>
				<text>当前已选中{{del_list.length}}个文件</text>
				<view  @tap='del_img' style="color:#DD524D;">删除 ({{del_list.length}})</view>
			</view>
			<view class="imgItem" v-for="(img,index) in imgList" :style="{transform:isdel&&del_list.includes(index)?' scale(0.9)':'scale(1)',boxShadow:isdel&&del_list.includes(index)?'0 0 0rpx 0rpx #eee':'0 0 10rpx 10rpx #eee'}" @tap="showimg(index)" @longtap='del(index)'>
				<image :src="img" mode="aspectFill"></image>
			</view>
			<view class="add_icon" :style="{height:!isdel?'200rpx':'0'}" @tap='add_img' >
				+
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		props: {
			data: {
				type: Array,
				default: []
			},
		},
		data() {
			return {
				del_list:[],
				isdel:false,
				imgList: []
			};
		},
		watch:{
			data: {
			      handler(val) {
					  this.imgList = this.data
			      },
			      immediate: true,
			      deep: true
			    }
		},
		created() {
			this.imgList = this.data
		},
		methods: {
			del_img(){
				let list = [...this.imgList]
				for(let i of this.del_list){
					list[i] = false
				}
				let arr = []
				for(let j of list){
					if(j){
						arr=[...arr,j]
					}
				}
				this.$set(this,'imgList',arr)
				this.del_list = []
				this.isdel = false
				this.update()
			},
			
			cancel_del(){
				this.del_list = []
				this.isdel = false
			},
			
			del(index){
				this.isdel = true
				this.del_list = [...this.del_list,index]
			},
			add_img() {
				if(this.isdel){
					return
				}
				let list = [...this.imgList]
				uni.chooseImage({
					success: (img) => {
						if (img.tempFilePaths && img.tempFilePaths.length > 0) {
							list = [...img.tempFilePaths, ...list]
						}
						this.$set(this, 'imgList', list)
						this.update()
					}
				})
			},
			showimg(index) {
				if(this.isdel){
					if(this.del_list.indexOf(index) == -1){
						this.del_list = [...this.del_list,index]
					}else{
						let ind = this.del_list.indexOf(index)
						let list = [...this.del_list]
						list.splice(ind,1)
						this.$set(this,'del_list',list)
					}
				}else{
					uni.previewImage({
						urls: this.imgList,
						indicator: 'Number',
						current: index
					})
				}

			},
			update(){
				this.$emit('update:data', this.imgList)
				this.$emit('change', this.imgList)
			}
		}
	}
</script>

<style lang='scss' scoped>
	.delbar{
		background-color: #fff;
		justify-content: center;
		font-size: 14px;
		color: #999;
		height: 100rpx;
		overflow: hidden;
		margin-top: 20rpx;
		min-width: 80%;
		align-items: center;
		flex: 1;
		display: flex;
		transition: 0.2s;
		view{
			display: flex;
			height: 100%;
			align-items: center;
			padding: 0 20rpx;
			color: #333;
			&:active{
				background-color: #f2f2f2;
			}
		}
		text{
			font-size: 12px;
			flex: 1;
			color: #999;
			text-align: center;
		}
	}
	
	.img_list {
		padding: 0 30rpx;
		display: flex;
		justify-content: space-between;
		flex-wrap: wrap;
		.imgItem {
			margin-bottom: 20rpx;
			min-width: 48%;
			height: 200rpx;
			overflow: hidden;
			border-radius: 15rpx;
			display: flex;
			transition: 0.5s;
			box-shadow: 0 0 10rpx 10rpx #eee;
			&:active {
				transform: scale(0.90);
			}

			image {
				border-radius: 15rpx;
				width: 100%;
				height: 100%;
			}
		}

		.add_icon {
			display: flex;
			min-width: 48%;
			height: 200rpx;
			border-radius: 15rpx;
			background-color: #f2f2f2;
			font-size: 50px;
			font-weight: bold;
			color: #FFFFFF;
			justify-content: center;
			align-items: center;
			transition: 0.2s;

			&:active {
				transform: scale(0.95);
				opacity: 0.7;
			}
		}
		.del_icon {
			height: 100rpx;
			overflow: hidden;
			margin-top: 20rpx;
			min-width: 80%;
			flex: 1;
			display: flex;
			transition: 0.2s;
			view{
				border-radius: 20rpx;
				flex: 1;
				display: flex;
				padding: 30rpx;
				background-color: #fff;
				justify-content: center;
				transition: 0.2s;
				&:active {
					transform: scale(0.95);
					opacity: 0.7;
				}
			}
		}
	}
</style>
